package com.base.exp

import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import com.base.ui.BaseActivity

fun Fragment.showToast(message: String) {
    val ctx = context ?: return
    ctx.showToast(message)
}

fun Fragment.runOnUiThread(runnable: Runnable) {
    val activity = activity ?: return
    activity.runOnUiThread(runnable)
}

private var alertDialog: AlertDialog? = null

fun BaseActivity.dialog(message: CharSequence?, isFinish: Boolean = false) {
    if (isFinishing || isDestroyed) return
    alertDialog?.dismiss()

    val alertBuilder = AlertDialog.Builder(this)
    alertBuilder.setTitle("温馨提示")
        .setMessage(message)
        .setCancelable(!isFinish)
        .setPositiveButton("确 定") { dialog, _ ->
            dialog?.dismiss()
            if (isFinish) {
                finish()
            }
        }

    alertDialog = alertBuilder.create()

    runOnUiThread { alertDialog?.show() }
}

fun AppCompatActivity.alert(
    content: CharSequence?,
    nextListener: DialogInterface.OnClickListener?,
    outsideCancel: Boolean = true
) {
    alert(content, "确 定", nextListener, "", null, outsideCancel)
}

fun AppCompatActivity.alert(
    content: CharSequence?,
    nextText: String?, nextListener: DialogInterface.OnClickListener?,
    cancelText: String?, cancelListener: DialogInterface.OnClickListener?,
    outsideCancel: Boolean = true
) {
    if (isFinishing || isDestroyed) return
    alertDialog?.dismiss()

    val alertBuilder = AlertDialog.Builder(this)
    alertBuilder.setTitle("温馨提示")
        .setMessage(content)
        .setCancelable(outsideCancel)

    if (null != nextListener)
        alertBuilder.setPositiveButton(nextText, nextListener)
    if (null != cancelListener)
        alertBuilder.setNegativeButton(cancelText, cancelListener)
    alertDialog = alertBuilder.create()

    runOnUiThread { alertDialog?.show() }
}

fun FragmentActivity.getController(navHostId: Int): NavController? {
    val navHostFragment = supportFragmentManager.findFragmentById(navHostId) as? NavHostFragment?
    return navHostFragment?.navController
}

private fun FragmentActivity.printMessage(navId: Int) {
    val navHostFragment =
        supportFragmentManager.findFragmentById(navId)
    val fragmentManager =
        navHostFragment?.childFragmentManager
    val fragments = fragmentManager?.fragments
//log(TAG, "fragment.size" + fragments?.size)
    val fragment = fragmentManager?.primaryNavigationFragment
//log(TAG, "fragment.name=>" + fragment?.javaClass?.name)
}

fun FragmentActivity.currentFragment(navId: Int): Fragment? {
    val navFragment =
        supportFragmentManager.findFragmentById(navId)
    return navFragment?.childFragmentManager?.primaryNavigationFragment
}

//Fragment
/*fun BaseFragment.dialog(message: CharSequence?, isFinish: Boolean = false) {
    val act = activity ?: return
    if (act.isFinishing || act.isDestroyed) return
    val alertDialogBuilder = AlertDialog.Builder(act)
        .setTitle("温馨提示")
        .setMessage(message)
        .setCancelable(!isFinish)
        .setPositiveButton("确 定") { dialog, _ ->
            dialog?.dismiss()
            if (isFinish) {
                finish()
            }
        }
    act.runOnUiThread { alertDialogBuilder.show() }
}

fun BaseFragment.alert(
    content: CharSequence?,
    nextListener: DialogInterface.OnClickListener?,
    outsideCancel: Boolean
) {
    alert(content, "确 定", nextListener, "", null, outsideCancel)
}

fun BaseFragment.alert(
    content: CharSequence?, nextText: String?, nextListener: DialogInterface.OnClickListener?,
    cancelText: String?, cancelListener: DialogInterface.OnClickListener?, outsideCancel: Boolean
) {
    val act = activity ?: return
    if (act.isFinishing || act.isDestroyed) return
    val alertDialogBuilder = AlertDialog.Builder(act)
        .setTitle("温馨提示")
        .setMessage(content)
        .setCancelable(outsideCancel)
    if (null != nextListener)
        alertDialogBuilder.setPositiveButton(nextText, nextListener)
    if (null != cancelListener)
        alertDialogBuilder.setNegativeButton(cancelText, cancelListener)
    act.runOnUiThread { alertDialogBuilder.show() }
}
*/